#!/usr/bin/perl

use strict;

package main;

use Getopt::Long;
use List::Util qw(first);
use Lunmgr ':all';

my %_cmds = (
    mount => '/bin/mount',
    umount => '/bin/umount',
);

my %_opts;

GetOptions(\%_opts,
          "-umount",
          "-all",
          "-help|h");

my $devtable = readDevtable("/home/lunmgr/lunmgr.devtable");
my @pathes = split(/,/, join(',', @ARGV));
my @entries = @$devtable if ($_opts{all});

if (! $_opts{all} && @pathes > 0) {
    foreach my $path (@pathes) {
        my $entry = first {$_->path eq $path} @$devtable;

        $entry ? push @entries, $entry
               : print STDERR "Invalid path $path\n";
    }
}

foreach my $entry (@entries) {
    next if ($entry->mount_pt eq "NONE");

    my $mpath = getMpathForUuid($entry->uuid); 
    if ($mpath) {
        my $cmd = $_opts{umount} 
                  ? $_cmds{umount} . " /dev/mapper/${mpath}p1"
                  : $_cmds{mount} . " /dev/mapper/${mpath}p1 " . $entry->path; 

        print "$cmd\n";
    }
    else {
        print STDERR "Path " . $entry->path . " is not visible for this cabinet\n";
    }
}

exit(0);
